<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
          <div v-show="this.$route.query.id" class="navigation">
              <span  @click="back">学科管理 > </span>
              <span>大数据 ></span>
              <span>标签管理</span>
          </div>
        <!-- 头部表单 -->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="标签名称">
            <el-input v-model="formInline.tagName"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="formInline.state" placeholder="请选择">
              <el-option label="启用" value="1">启用</el-option>
              <el-option label="禁用" value="0">禁用</el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="delSubmit" size="small">清除</el-button>
            <el-button type="primary" @click="onSubmit" size="small">搜索</el-button>
          </el-form-item>

          <!--返回目录按钮  -->
          <el-button
          v-show="this.$route.query.id"
          type="text"
          class="right"
          icon="el-icon-back"
          @click="back"
          >返回学科</el-button>
          <!-- this.$route.query.name &&  -->

          <!-- 新增学科 -->
          <el-button
            class="addSubject"
            type="success"
            icon="el-icon-edit"
            size="small"
            @click="addList">新增学科</el-button>
        </el-form>

        <!-- 表单下面的提示 -->
        <el-alert
          :title="`数据一共${amount}条`"
          type="info"
          show-icon
          :closable="false"
        ></el-alert>

        <!-- 表格渲染 -->
        <!-- 引入表单组件 -->
        <tags-from :table-data="tableData" @del="del" @revamp="addrevamp" :query="query" />

        <!-- 分页功能 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 20, 25]"
          :page-size="query.pagesize"
          layout="prev, pager, next, sizes,jumper"
          background
          :current-page="query.page"
          :total="amount"
        >
        </el-pagination>
      </el-card>

      <!-- 弹窗功能 -->
      <el-dialog
        v-if="dialogFormVisible"
        :title="isEndt ? '修改学科' : '添加学科'"
        :visible.sync="dialogFormVisible"
        width="450px">
        <tags-add @close="close" :is-endt="isEndt" :list-id="listId" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
// 引入表单组件
import tagsFrom from './companys/tagsFrom.vue'
import TagsAdd from '../components/tags-add.vue'
// 导入api
import { list } from '@/api/hmmm/tags'

export default {
  name: 'Tags',
  // 注册表单组件
  components: {
    tagsFrom,
    TagsAdd
  },
  data () {
    return {
      // 获取列表数据
      tableData: [],
      // 搜索数据
      formInline: {
        tagName: '', // 标签名称
        state: ''// 状态
      },
      // 页码&条数
      query: {
        page: 1, // 页码
        pagesize: 10 // 每页条数
      },
      dialogFormVisible: false, // 控制新增学科的显示与隐藏
      amount: 0, // 获取页面一共有多少列表项
      listId: {}, // 点击文章Id
      isEndt: false // 判断是否是编辑或添加
    }
  },
  created () {
    // this.getList()
    // 跳转页面时将上个页面的值赋值给本页面请求对应数据
    this.query.subjectID = this.$route.query.id
    // this.query.directoryName = this.$route.query.name
    this.getList() // 渲染页面
  },
  methods: {
    // 点击回退页面
    back () {
      // console.log(this.$route.query)
      this.$router.back()
    },

    // 编辑功能
    addrevamp (data) {
      this.dialogFormVisible = true
      this.isEndt = true
      this.listId = data
    },

    // 删除功能
    del () {
      this.getList()
    },

    // 确认后弹窗
    close () {
      // 添加成功后关闭弹窗
      this.dialogFormVisible = false
      // 重新渲染页面数据
      this.getList()
      // 列表清空
      this.formInline = {}
    },

    // 获取列表基本列表
    async getList () {
      const res = await list(this.query)
      // console.log(res)
      this.tableData = res.data.items
      this.amount = res.data.counts
    },

    // 新增学科
    async addList () {
      this.dialogFormVisible = true
      this.isEndt = false
    },

    // 搜索按钮
    async onSubmit () {
      const res = await list(this.formInline)
      this.tableData = res.data.items
      this.amount = res.data.counts
      // this.getList()
      this.formInline = {}
    },

    // 清除按钮
    delSubmit () {
      this.formInline = {}
      this.getList()
    },

    handleSizeChange (newSize) {
      // console.log(newSize)
      this.query.pagesize = newSize
      this.getList()
    },

    handleCurrentChange (newPage) {
      // console.log(newPage)
      this.query.page = newPage
      this.getList()
    }
  }
  // 设置导航守卫
  // beforeRouteUpdate (to, from, next) {
  //   this.query.directoryName = null
  //   this.query.subjectID = null
  //   this.getList()
  //   next()
  // }

}
</script>

<style scoped>
.addSubject {
  position: absolute;
  right: 42px;
}
.el-pagination {
  text-align: right;
  margin-top: 20px;
}
.right{
    position: absolute;
    right: 160px;
}
.navigation{
    margin-bottom: 15px;
    font-size: 14px;
}
</style>
